package mate.business
{
	import com.asfusion.mate.core.GlobalDispatcher;
	
	import mate.events.RegistrationEvent;
	
	import spark.components.NavigatorContent;
	
	public class RegistrationManager
	{
		import classespinkpony.*;
		
		import mx.collections.ArrayCollection;
		import mx.controls.Alert;
		import mx.rpc.Fault;
		
		
		private var _salutations:ArrayCollection = new ArrayCollection();
		
		
		//Practices address fields
		private var _districtsPrac1:ArrayCollection = new ArrayCollection();
		private var _districtsPrac2:ArrayCollection = new ArrayCollection();
		private var _districtsPrac3:ArrayCollection = new ArrayCollection();
		
		private var _locationsPrac1:ArrayCollection = new ArrayCollection();
		private var _locationsPrac2:ArrayCollection = new ArrayCollection();
		private var _locationsPrac3:ArrayCollection = new ArrayCollection();
		//End practices address fields
		
		//Invoice address fields
		private var _invDistricts:ArrayCollection = new ArrayCollection();
		private var _invLocations:ArrayCollection = new ArrayCollection();
		//End invoice address fields
		
		private var _specializations:ArrayCollection = new ArrayCollection();
		private var _targets: ArrayCollection = new ArrayCollection();
		private var _problems: ArrayCollection = new ArrayCollection();
		private var _professions: ArrayCollection = new ArrayCollection();
		
		
		private var _user:User = new User();
		private var _address:Address = new Address();
		private var _inv_address:Address = new Address();
		private var _practices:ArrayCollection = new ArrayCollection();
		
		private var disp: GlobalDispatcher;
		
		public function RegistrationManager(){
			disp = new GlobalDispatcher();
		}
		
		
		[Bindable]
		public var selectedView: int = 0;
		
		[Bindable]
		public function get professions():ArrayCollection
		{
			return _professions;
		}

		public function set professions(value:ArrayCollection):void
		{
			_professions = value;
		}

		[Bindable]
		public function get invLocations():ArrayCollection
		{
			return _invLocations;
		}

		public function set invLocations(value:ArrayCollection):void
		{
			_invLocations = value;
		}

		[Bindable]
		public function get invDistricts():ArrayCollection
		{
			return _invDistricts;
		}

		public function set invDistricts(value:ArrayCollection):void
		{
			_invDistricts = value;
		}

		[Bindable]
		public function get address():Address
		{
			return _address;
		}
		
		public function set address(value:Address):void
		{
			_address = value;
		}
		
		[Bindable]
		public function get inv_address():Address
		{
			return _inv_address;
		}
		
		public function set inv_address(value:Address):void
		{
			_inv_address = value;
		}
		
		public function handleFault(fault:Fault):void{
			Alert.show(fault.faultString, fault.faultCode.toString());
		}
		
		public function storeLocations(locations:Object, componentId: String):void
		{
			var temp:ArrayCollection = new ArrayCollection();
			
			if(locations != null){
				for each (var location:Location in locations)
				{
					temp.addItem(location);
				}
			}
			switch(componentId){
				case "prac1":
					locationsPrac1 = temp;
					break;
				case "prac2":
					locationsPrac2 = temp;
					break;
				case "prac3":
					locationsPrac3 = temp;
					break;
				case "invoice":
					invLocations = temp;
					break;
			}
		}
		
		public function storeDistricts(districts:Object, componentId: String):void
		{
			var temp:ArrayCollection = new ArrayCollection();
			
			if(districts != null){
				for each (var district:District in districts)
				{
					temp.addItem(district);
				}
			}
			switch(componentId){
				case "prac1":
					districtsPrac1 = temp;
					break;
				case "prac2":
					districtsPrac2 = temp;
					break;
				case "prac3":
					districtsPrac3 = temp;
					break;
				case "invoice":
					invDistricts = temp;
					break;
			}
			
		}		
		
		public function storeSpecializations(specializations:Object):void
		{
			var temp:ArrayCollection = new ArrayCollection();
			
			if(specializations != null){
				for each (var spec:Specialization in specializations)
				{
					temp.addItem(spec);
				}
			}
			
			this.specializations = temp;
		}	
		
		public function storeTargets(targets:Object):void
		{
			var temp:ArrayCollection = new ArrayCollection();
			
			if(targets != null){
				for each (var target:Target in targets)
				{
					temp.addItem(target);
				}
			}
			
			this.targets = temp;
		}
		
		public function storeProblems(problems:Object):void
		{
			var temp:ArrayCollection = new ArrayCollection();
			
			if(problems != null){
				for each (var prob:Problem in problems)
				{
					temp.addItem(prob);
				}
			}
			
			this.problems = temp;
		}
		
		public function storeProfessions(professions:Object):void
		{
			var temp:ArrayCollection = new ArrayCollection();
			
			if(professions != null){
				for each (var prof:Profession in professions)
				{
					temp.addItem(prof);
				}
			}
			
			this.professions = temp;
		}
		
		[Bindable]
		public function get salutations():ArrayCollection
		{
			return _salutations;
		}
		
		public function set salutations(value:ArrayCollection):void
		{
			_salutations = value;
		}
		
		
		[Bindable]
		public function get user():User
		{
			return _user;
		}
		
		public function set user(value:User):void
		{
			_user = value;
		}
		
		public function storeUser(user:Object):void
		{
			if(user != null){
				for each (var u:User in user)
				{
					this.user = u;	
				}				
			}			
		}
		
		public function saveInvoiceAddress(): void{
			
			var saveInvoiceAddressEvent: RegistrationEvent = new RegistrationEvent(RegistrationEvent.SAVEINVOICEADDRESSTODB);
			saveInvoiceAddressEvent.address = inv_address;
			
			disp.dispatchEvent(saveInvoiceAddressEvent);
		}
		
		public function saveUser(invoiceAddressId: Object): void{
			
			user.invoice_address_id = invoiceAddressId.toString();
			
			var saveUserEvent: RegistrationEvent = new RegistrationEvent(RegistrationEvent.SAVEUSERTODB);
			saveUserEvent.user = user;
			
			disp.dispatchEvent(saveUserEvent);
		}
		
		
		public function saveUserPractices(userId: Object): void{
			//Loopen over alle practices en voor elk een event aanroepen
			for(var i : int = 0; i < practices.length;i++){
				var prac : Practice = practices.getItemAt(i) as Practice;
				prac.user_id = userId.toString();
				
				
				
				var savePracticeAddressEvent: RegistrationEvent = new RegistrationEvent(RegistrationEvent.SAVEPRACTICEADDRESSTODB);
				savePracticeAddressEvent.address = prac.address;
				//make the event remember the index of the current practice in our list of practices, to forward it to the next event
				savePracticeAddressEvent.searchNumber = i;
				
				disp.dispatchEvent(savePracticeAddressEvent);
			}
		}
		
		public function savePractice(practiceAddressId:Object, practiceIndex: int):void{
			var prac: Practice = practices.getItemAt(practiceIndex) as Practice;
			prac.address_id = practiceAddressId.toString();
			var savePracticeEvent: RegistrationEvent = new RegistrationEvent(RegistrationEvent.SAVEPRACTICETODB);
			savePracticeEvent.practice = prac;
			
			disp.dispatchEvent(savePracticeEvent);
		}
		
		
		
		public function debug_alert(message:Object): void{
			Alert.show(message.toString());
		}

		[Bindable]
		public function get practices():ArrayCollection
		{
			return _practices;
		}

		public function set practices(value:ArrayCollection):void
		{
			_practices = value;
			for each (var practice:Practice in _practices)
			{
				practice.geocodeLatLng();
			}
		}

		[Bindable]
		public function get specializations():ArrayCollection
		{
			return _specializations;
		}

		public function set specializations(value:ArrayCollection):void
		{
			_specializations = value;
		}

		[Bindable]
		public function get targets():ArrayCollection
		{
			return _targets;
		}

		public function set targets(value:ArrayCollection):void
		{
			_targets = value;
		}

		[Bindable]
		public function get problems():ArrayCollection
		{
			return _problems;
		}

		public function set problems(value:ArrayCollection):void
		{
			_problems = value;
		}

		[Bindable]
		public function get districtsPrac1():ArrayCollection
		{
			return _districtsPrac1;
		}

		public function set districtsPrac1(value:ArrayCollection):void
		{
			_districtsPrac1 = value;
		}

		[Bindable]
		public function get districtsPrac2():ArrayCollection
		{
			return _districtsPrac2;
		}

		public function set districtsPrac2(value:ArrayCollection):void
		{
			_districtsPrac2 = value;
		}

		[Bindable]
		public function get districtsPrac3():ArrayCollection
		{
			return _districtsPrac3;
		}

		public function set districtsPrac3(value:ArrayCollection):void
		{
			_districtsPrac3 = value;
		}

		[Bindable]
		public function get locationsPrac1():ArrayCollection
		{
			return _locationsPrac1;
		}

		public function set locationsPrac1(value:ArrayCollection):void
		{
			_locationsPrac1 = value;
		}

		[Bindable]
		public function get locationsPrac2():ArrayCollection
		{
			return _locationsPrac2;
		}

		public function set locationsPrac2(value:ArrayCollection):void
		{
			_locationsPrac2 = value;
		}

		[Bindable]
		public function get locationsPrac3():ArrayCollection
		{
			return _locationsPrac3;
		}

		public function set locationsPrac3(value:ArrayCollection):void
		{
			_locationsPrac3 = value;
		}
		
		
	}
}